//
//  OwnerSignForNewStcFormView.swift
//  ezyform
//
//  Created by Moennig Q on 7/5/2022.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import SnapKit
class OwnerSignForNewStcFormView: GreenNaviBarView {
    let signView = SignatureView()
    //
    let signInfoView = UIView()
    let signFullNameTitle = ContentLabel()
    let signFullNameLabel = ContentLabel(hexStr: "#292D32B2")
    let signDateLabel = ContentLabel(hexStr: "#292D32B2")
    
    var operateBtn = OperationButton(R.string.localizable.save())
    var operateBtnClousure: (()->Void)?
    //
    //owner
    let signedBackView = UIView()
    let signedTitle = LabelWithRedStarView()
    let signedTextfield = TextFieldWithRequiredView()
    //
  
    override func setUpView() {
        let contentBackView = UIView()
        contentBackView.drawCorner(byRoundingCorners: [.topLeft,.topRight])
        self.addSubview(contentBackView)
        contentBackView.snp.makeConstraints { make in
            make.top.equalTo(topBackView.snp.bottom).offset(-20)
            make.left.right.bottom.equalToSuperview()
        }
        
        contentBackView.addSubview(operateBtn)
        operateBtn.snp.makeConstraints { (make) in
            make.bottom.equalTo(OperationButton.bottomMargin)
            make.height.equalTo(OperationButton.height)
            make.left.equalTo(16)
            make.right.equalTo(-16)
        }
        //底部显示的签名信息
        contentBackView.addSubview(signInfoView)
        signInfoView.snp.makeConstraints { (make) in
            make.bottom.equalTo(operateBtn.snp.top).offset(-10)
            make.left.right.equalTo(operateBtn)
            make.height.greaterThanOrEqualTo(0)
            
        }
        //sign full name
        signFullNameTitle.textAlignment = .left
        signFullNameTitle.numberOfLines = 0
        signInfoView.addSubview(signFullNameTitle)
        signFullNameTitle.snp.makeConstraints { (make) in
            make.top.left.equalToSuperview()
            make.width.equalTo(120)
            make.height.greaterThanOrEqualTo(0)
        }
        signFullNameLabel.textAlignment = .right
        signFullNameLabel.numberOfLines = 0
        signInfoView.addSubview(signFullNameLabel)
        signFullNameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(signFullNameTitle.snp.right)
            make.centerY.equalTo(signFullNameTitle)
            make.height.greaterThanOrEqualTo(signFullNameLabel)
            make.right.equalToSuperview()
        }
        //sign date
        let signDateTitle = ContentLabel()
        signDateTitle.text = R.string.localizable.signatureDate()
        signDateTitle.textAlignment = .left
        signInfoView.addSubview(signDateTitle)
        signDateTitle.snp.makeConstraints { (make) in
            make.top.equalTo(signFullNameLabel.snp.bottom).offset(12)
            make.left.bottom.equalToSuperview()
            make.height.width.greaterThanOrEqualTo(0)
        }
        //
        signDateLabel.textAlignment = .right
        signInfoView.addSubview(signDateLabel)
        signDateLabel.snp.makeConstraints { (make) in
            make.left.equalTo(signDateTitle.snp.right)
            make.right.equalToSuperview()
            make.centerY.equalTo(signDateTitle)
            make.height.greaterThanOrEqualTo(0)
        }
        //顶部的owner name 编辑框
        contentBackView.addSubview(signedBackView)
        signedBackView.snp.makeConstraints { (make) in
            make.top.equalTo(20)
            make.left.right.equalTo(operateBtn)
            make.height.greaterThanOrEqualTo(0)
        }
        //
        signedBackView.addSubview(signedTitle)
        signedTitle.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview()
            make.width.height.greaterThanOrEqualTo(0)
        }
        //
        signedTextfield.defaultTipMsg = R.string.localizable.pleaseTypeOwnerSName()
        signedBackView.addSubview(signedTextfield)
        signedTextfield.snp.makeConstraints { (make) in
            make.top.equalTo(signedTitle.snp.bottom).offset(12)
            make.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview()
        }
        //sign view
        signView.layer.cornerRadius = Constant.BackViewCornerRadius
        signView.backgroundColor = .white
        signView.clipsToBounds = true
        signView.layer.borderWidth = 1
        signView.layer.borderColor = UIColor(hexStr: "#292D3233").cgColor
        contentBackView.addSubview(signView)
        signView.snp.makeConstraints { (make) in
            make.bottom.equalTo(signInfoView.snp.top).offset(-20)
            make.top.equalTo(signedBackView.snp.bottom).offset(12)
            make.left.right.equalTo(operateBtn)
        }
        
        let clearBtn = BaseButton()
        clearBtn.setTitle(R.string.localizable.clear(), for: UIControl.State.normal)
        clearBtn.setTitleColor(UIColor(hexStr: Constant.ThemeColorHexStr), for: UIControl.State.normal)
        clearBtn.setFont(R.font.interMedium(size: 14))
        contentBackView.addSubview(clearBtn)
        clearBtn.snp.makeConstraints { (make) in
            make.right.bottom.equalTo(signView)
            make.height.equalTo(44)
            make.width.equalTo(60)
        }
        clearBtn.addTarget(self, action: #selector(clearSignature), for: UIControl.Event.touchUpInside)
        
        operateBtn.addTarget(self, action: #selector(operateBtnClick(_:)), for: UIControl.Event.touchUpInside)
    }
    func setData(_ model: PVDDetailModel, asWitnessFlag: Bool){
        //sign date
        let signDate = DateUtil().getDatetimeStr(Date(), format: "MMM dd, yyyy")
        self.signDateLabel.text = signDate
        if asWitnessFlag {//owner是其他签名的witness
            operateBtn.titleLabel?.text = R.string.localizable.save()
        }else{//不是
            operateBtn.titleLabel?.text = R.string.localizable.next()
        }
        //个人用户显示owner name
        if model.owner_address.type.lowercased() == "individual" {
            self.signedBackView.isHidden = true
            let signFullName = model.signatures.owner_full_name
            self.signFullNameTitle.text = R.string.localizable.signedBy()
            self.signFullNameLabel.text = signFullName
            //
            signView.snp.remakeConstraints { (make) in
                make.bottom.equalTo(signInfoView.snp.top).offset(-20)
                make.top.equalTo(20)
                make.left.right.equalTo(operateBtn)
            }
        }else{//非个人用户输入，且默认owner full name
            signView.snp.remakeConstraints { (make) in
                make.bottom.equalTo(signInfoView.snp.top).offset(-20)
                make.top.equalTo(signedBackView.snp.bottom).offset(12)
                make.left.right.equalTo(operateBtn)
            }
            self.signedBackView.isHidden = false
            self.signedTitle.titleLabel.text = R.string.localizable.ownerName()
            self.signedTextfield.textField.text = model.signatures.owner_full_name
        }
    }
    @objc func clearSignature(){
        self.signView.clearSignature()
    }
    @objc func operateBtnClick(_ sender: UIButton){
        self.operateBtnClousure?()
    }
}
